package com.huazi.www.user.cost.dao.impl;

import com.huazi.www.user.cost.dao.UserDao;
import com.huazi.www.user.cost.entity.UserEntity;
import com.huazi.www.user.cost.servlet.UserServlet;
import com.huazi.www.user.cost.util.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserImpl extends BaseDao implements UserDao {

    /**
     * 缴费状态查询总记录数
     *
     * @return 放回int类型
     */
    @Override
    public int UserTotalPageCount() {
        String sql = "SELECT COUNT(1) FROM department_queue WHERE state=0";
        ResultSet rs = null;
        int count = 0;
        rs = executeQuery(sql);
        try {
            rs.next();
            count = rs.getInt(1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    /**
     * 按照缴费状态查询user信息
     *
     * @param start      起始下标
     * @param lineNumber 显示的行数
     * @return 放回List<UserEntity></>类型
     */
    @Override
    public List<UserEntity> UserSelect(String name, Integer start, Integer lineNumber) {
        String sql = "SELECT qu.id,qu.userId,card.`name`,qu.userTime,pr.checkxm,qu.state \n" +
                "FROM department_queue AS qu,frontdesk_card AS card,frontdesk_price AS pr \n" +
                "WHERE qu.userId=card.id AND qu.userType=pr.checkxm AND qu.state=0 ";
        ResultSet rs = null;
        List<UserEntity> list = new ArrayList<>();

        if (name.equals("")) {
            sql += " LIMIT ?,?";
            rs = executeQuery(sql, start, lineNumber);
        } else {
            sql += "and card.`name`=?  LIMIT ?,? ";
            rs = executeQuery(sql, name, start, lineNumber);
        }
        try {
            while (rs.next()) {
                UserEntity entity = new UserEntity();
                entity.setId(rs.getInt(1));
                entity.setUserId(rs.getInt(2));
                entity.setName(rs.getString(3));
                entity.setUserTime(rs.getString(4));
                entity.setType(rs.getString(5));
                entity.setState(rs.getInt(6));
                list.add(entity);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    /**
     * 查询用户缴费状态
     *
     * @return 放回int类型
     */
    @Override
    public int UserStateSelect() {
        String sql = "SELECT state FROM department_queue WHERE state=0";
        ResultSet rs = executeQuery(sql);
        int count = 0;
        try {
            rs.next();
            count = rs.getInt(1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    /**
     * 根据用户id查询用户余额
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public Double UserMoneySelect(Integer userId) {
        String sql = "SELECT money FROM department_queue AS qu,frontdesk_card AS card WHERE qu.userId=card.id AND qu.userId=? AND qu.state=0";
        ResultSet rs = null;
        double money = 0;
        rs = executeQuery(sql, userId);
        try {
            rs.next();
            money = rs.getInt(1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return money;
    }

    /**
     * 根据用户id查询用户缴费信息
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public List<UserEntity> userPayment(Integer userId) {
        String sql = "SELECT qu.userId,qu.userType FROM department_queue AS qu,frontdesk_card AS card WHERE qu.userId=card.id AND qu.userId=? AND qu.state=0";
        ResultSet rs = null;
        List<UserEntity> list = new ArrayList<>();
        rs = executeQuery(sql, userId);
        try {
            while (rs.next()) {
                UserEntity entity = new UserEntity();
                entity.setUserId(rs.getInt(1));
                entity.setType(rs.getString(2));
                list.add(entity);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    /**
     * 根据用户名查询索要缴费的类型价钱
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public Double ctPrice(Integer userId) {
        String sql = "SELECT pr.price FROM frontdesk_price AS pr,department_queue AS qu WHERE qu.userType=pr.checkxm AND qu.userId=? AND qu.state=0";
        ResultSet rs = null;
        double price = 0;
        rs = executeQuery(sql, userId);
        try {
            rs.next();
            price = rs.getInt(1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return price;
    }

    /**
     * 缴费成功后的用户缴费信息
     *
     * @return
     */
    @Override
    public int userPaymentInsert(UserEntity entity, Integer userId, double money) {
//        double money=UserMoneySelect(userId);
        String type = "前台缴费";
        String sql = "INSERT INTO consumeinfo(cardNo,consumeType,drugType,money) VALUES(?,?,?,?)";
        int count = executeUpdate(sql, entity.getUserId(), entity.getType(), type, money);

        //修改用户余额
        String str = "UPDATE frontdesk_card set money=money-? where id=?";
        executeUpdate(str, money, entity.getUserId());

        return count;
    }

    /**
     * 根据用户id修改用户缴费状态
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public int userStateUpdate(Integer userId) {
        String sql = "UPDATE department_queue SET state=1 WHERE userId=?";
        int count = executeUpdate(sql, userId);
        return count;
    }

    /**
     * 根据用户id查找要缴费的总价钱
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public int userPaymentNbm(Integer userId) {
        String sql = "SELECT COUNT(1) FROM department_queue WHERE userId=? AND state=0";
        ResultSet rs = null;
        int count = 0;
        rs = executeQuery(sql, userId);
        try {
            rs.next();
            count = rs.getInt(1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return count;
    }
}
